package com.fymxy.question_bank_management.controller;

import com.fymxy.question_bank_management.pojo.ApiResult;
import com.fymxy.question_bank_management.pojo.Keyword;
import com.fymxy.question_bank_management.pojo.ProblemWithBLOBs;
import com.fymxy.question_bank_management.service.KeywordService;
import com.fymxy.question_bank_management.service.ProblemService;
import com.fymxy.question_bank_management.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class ProblemManageController {

    @Autowired
    private ProblemService problemService;

    @Autowired
    private KeywordService keywordService;


    //在进入插入试题的页面后就执行,获取已有知识点
    @GetMapping("teacher/getAllKeywordContent")
    public ApiResult getAllKeywordContent(){
        //getKeywords
        List<String> KeywordRes = keywordService.getAllKeywordContent();
        if(KeywordRes != null){
            return ApiResultHandler.buildApiResult(200,"请求成功",KeywordRes);
        }
        return ApiResultHandler.buildApiResult(400,"请求失败",KeywordRes);
    }


    //插入一道题目
    @PostMapping("teacher/addProblem")
    public ApiResult addProblem(@RequestBody ProblemWithBLOBs problem){
        int problemRes = problemService.addProblem(problem);
        if(problemRes == 1){
            return ApiResultHandler.buildApiResult(200, "请求成功",problemRes);
        }
        return ApiResultHandler.buildApiResult(400,"请求失败",problemRes);
    }

    //在插入试题后执行,插入若干知识点
    @PostMapping("teacher/addKeywords")
    public ApiResult addKeyword(@RequestBody Map<Integer,String> keywordContents){

        //刚插入的试题编号
        int problemNo = problemService.getTopProblemNo();
        //插入条数
        int keywordRes = 0;
        for(String keywordContent :keywordContents.values() ){
            //组装keyword记录
            if (!keywordContent.equals("")) {
                Keyword keyword = new Keyword();
                keyword.setProblemNo(problemNo);
                keyword.setKeywordContent(keywordContent);
                //插入keyword表,并记录影响条数
                keywordRes += keywordService.insert(keyword);
            }
        }
        System.out.println("插入keyword表"+keywordRes+"条");
        if(keywordRes >= 1){
            return ApiResultHandler.buildApiResult(200,"请求成功",keywordRes);
        }
        return ApiResultHandler.buildApiResult(400,"请求失败",keywordRes);
    }


}